package com.hdw.cms.service.impl; 

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import net.coobird.thumbnailator.geometry.Positions;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;

import com.hdw.cms.service.AttachmentService;
import com.hdw.cms.utils.UeditorUtil;
import com.hdw.basic.model.Pager;
import com.hdw.basic.model.SystemContext;
import com.hdw.cms.dao.AttachmentDao;
import com.hdw.cms.model.Attachment;
import com.hdw.cms.model.IndexPic;
import com.hdw.cms.model.Topic;

@Service("attachmentService")
public class AttachmentServiceImpl implements AttachmentService {

	public final static int IMG_WIDTH = 900;
	public final static int THUMBNAIL_WIDTH = 150;
	public final static int THUMBNAIL_HEIGHT = 110;
	public final static int THUMBNAIL_NewWIDTH = 370;
	public final static int THUMBNAIL_NewHEIGHT = 255;

	@Resource(name = "attachmentDao")
	private AttachmentDao attachmentDao;

	@Override
	public void add(Attachment a, InputStream is) throws IOException {
		try {
			
			attachmentDao.add(a);
			addFile(a, is);
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}
	}

	private void addFile(Attachment a, InputStream is) throws IOException {
		// 进行文件的存储
		String realPath = SystemContext.getRealPath();
		String path = realPath + "/resources/upload/";
		String thumbPath = realPath + "/resources/upload/thumbnail/";
		String NewthumbPath = realPath + "/resources/upload/Newthumbnail/";
		File fp = new File(path);
		File tfp = new File(thumbPath);
		File ntfp = new File(NewthumbPath);
		// System.out.println(fp.exists());
		// System.out.println(tfp.exists());
		if (!fp.exists())
			fp.mkdirs();
		if (!tfp.exists())
			tfp.mkdirs();
		if (!ntfp.exists())
			ntfp.mkdirs();
		path = path + a.getNewName();
		thumbPath = thumbPath + a.getNewName();
		NewthumbPath = NewthumbPath + a.getNewName();
		// System.out.println(path+","+thumbPath);
		if (a.getisProImg() == 1 || a.getIsVideoImg() == 1) {
			BufferedImage oldBi = ImageIO.read(is);
			int width = oldBi.getWidth();
			Builder<BufferedImage> bf = Thumbnails.of(oldBi);
			if (width > IMG_WIDTH) {
				bf.scale((double) IMG_WIDTH / (double) width);
			} else {
				bf.scale(1.0f);
			}
			bf.toFile(path);
			// 缩略图的处理
			// 1、将原图进行压缩

			BufferedImage tbi = Thumbnails.of(oldBi).scale((THUMBNAIL_WIDTH * 1.2) / width).asBufferedImage();
			// 2、进行切割并且保存
			Thumbnails.of(tbi).scale(1.0f).sourceRegion(Positions.CENTER, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT)
					.toFile(thumbPath);

			// 缩略图的处理--用于首页新闻图片轮播
			Thumbnails.of(oldBi).forceSize(THUMBNAIL_NewWIDTH, THUMBNAIL_NewHEIGHT).toFile(NewthumbPath);

		} else {
			FileUtils.copyInputStreamToFile(is, new File(path));
		}
	}

	@Override
	public void delete(int id) {
		Attachment a=attachmentDao.load(id);
		String[] filename = UeditorUtil.dealUeFileName(a.getUnUseFile());
		if (filename != null) {
			String realpath = SystemContext.getRealPath() + "/upload/";
			for (int i = 0; i < filename.length; i++) {
				new File(realpath + filename[i]).delete();
			}
		}
		attachmentDao.delete(id);
	}

	@Override
	public Attachment load(int id) {
		return attachmentDao.load(id);
	}

	@Override
	public List<Attachment> listIndexNewPicByNum() {
		return this.attachmentDao.listIndexNewPicByNum();
	}

	@Override
	public void updatePro(String proName, String proMessage, String proPar, int id) {
		attachmentDao.updetePro(proName, proMessage, proPar, id);
	}

	@Override
	public Pager<Attachment> findProPic(Integer proTypeId) {
		return attachmentDao.findProPic(proTypeId);
	}

	@Override
	public Pager<Attachment> findVideo(Integer videoTypeId) {
		return attachmentDao.findVideo(videoTypeId);
	}

	@Override
	public void updateIndexImg(Integer id, Integer isIndex) {
		attachmentDao.updateIndexImg(id, isIndex);
	}

	@Override
	public void updatePic(Integer proTypeId, String attName, String picMessage, String proPar, int id) {
		attachmentDao.updatePic(proTypeId, attName, picMessage, proPar, id);
	}

	@Override
	public Attachment loadVideo(int id) {
		return attachmentDao.loadVideo(id);
	}

	@Override
	public void updateVideo(Integer proTypeId, String attName, String picMessage, int id) {
		attachmentDao.updateVideo(proTypeId, attName, picMessage, id);
	}

	@Override
	public Pager<Attachment> listSearchPro(String attName) {
		return attachmentDao.listSearchPro(attName);
	}

	@Override
	public Map<String, Integer> getMinAdnMaxProPos() {
		return attachmentDao.getMinAdnMaxProPos();
	}

	@Override
	public Map<String, Integer> getMinAdnMaxVdoPos() {
		return attachmentDao.getMinAdnMaxVdoPos();
	}

	@Override
	public void updateProPos(int id, int oldPos, int newPos) {
		attachmentDao.updateProPos(id, oldPos, newPos);
	}

	@Override
	public void updateVdoPos(int id, int oldPos, int newPos) {
		attachmentDao.updateVdoPos(id, oldPos, newPos);
	}

	@Override
	public List<Attachment> findVideoBytype(Integer videoTypeId) {
		return attachmentDao.findVideoBytype(videoTypeId);
	}

	@Override
	public void updatePath(Attachment t) {
		attachmentDao.updatePath(t);
	}

	@Override
	public List<Attachment> listVideoAndPro() {
		return attachmentDao.listProAndVideo();
	}

	@Override
	public List<Attachment> listAttByProType(int typeId) {
		return attachmentDao.listAttByProType(typeId);
	}
}
